import 'package:flutter/material.dart';
import 'package:flutter_deer/account/presenter/user_bank_presenter.dart';
import 'package:flutter_deer/mvp/base_page.dart';
import 'package:flutter_deer/mvp/power_presenter.dart';
import 'package:flutter_deer/res/constant.dart';
import 'package:flutter_deer/res/resources.dart';
import 'package:flutter_deer/routers/fluro_navigator.dart';
import 'package:flutter_deer/util/change_notifier_manage.dart';
import 'package:flutter_deer/util/image_utils.dart';
import 'package:flutter_deer/widgets/load_image.dart';
import 'package:flutter_deer/widgets/my_app_bar.dart';
import 'package:flutter_deer/account/iview/user_bank_iview.dart';
import 'package:flutter_deer/account/provider/user_bank_provider.dart';
import 'package:flutter_deer/account/models/user_bank_entity.dart';
import 'package:provider/provider.dart';
import '../account_router.dart';

/// design/6店铺-账户/index.html#artboard7
class WithdrawalAccountListPage extends StatefulWidget {

  const WithdrawalAccountListPage({super.key});

  @override
  _WithdrawalAccountListPageState createState() => _WithdrawalAccountListPageState();
}

class _WithdrawalAccountListPageState extends State<WithdrawalAccountListPage> with BasePageMixin<WithdrawalAccountListPage, PowerPresenter>, AutomaticKeepAliveClientMixin<WithdrawalAccountListPage>,ChangeNotifierMixin<WithdrawalAccountListPage> implements UserBankIMvpView {
  
  final int _selectIndex = 0;
  late UserBankPagePresenter userBankPagePresenter;
  UserBankProvider provider = UserBankProvider();
  @override
  bool get wantKeepAlive => false;

  @override
  PowerPresenter<dynamic> createPresenter() {
    final PowerPresenter<dynamic> powerPresenter = PowerPresenter<dynamic>(this);
    userBankPagePresenter = new UserBankPagePresenter();
    powerPresenter.requestPresenter([userBankPagePresenter]);
    return powerPresenter;
  }

  @override
  void setUserBankList(List<UserBankEntity>? userBankList){
    provider.setUserBankList(userBankList);
  }
  @override
  void saveUserBankSuccess(){

  }
  @override
  void saveUserBankFail(){

  }

  @override
  void removeUserBankSuccess(){
    Future.delayed(Duration(seconds: 1),()=>{userBankPagePresenter.fetchUserBankList()});
  }
  
  @override
  void initState() {
    super.initState();
    if(mounted) {
      Future.delayed(Duration(seconds: 1), () =>
      {
        userBankPagePresenter.fetchUserBankList()
      });
    }
  }

  @override
  void dispose() {
    // provider.dispose();
    super.dispose();
  }

  @override
  Map<ChangeNotifier, List<VoidCallback>?>? changeNotifier() {
    print('changeNotifier changeNotifier changeNotifier changeNotifier changeNotifier changeNotifier changeNotifier changeNotifier changeNotifier');
    print(provider.userBankList?.length);
  }
  
  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider<UserBankProvider>(
      create: (_) => provider,
      child: Scaffold(
        appBar: MyAppBar(
            centerTitle: '选择账号',
            actionName: '添加',
            onPressed: () => NavigatorUtils.push(context, AccountRouter.addWithdrawalAccountPage)
        ),
        body: Consumer<UserBankProvider>(
          builder: (_,provider,child){
            return ListView.separated(
              itemCount: provider?.userBankList?.length??0,
              separatorBuilder: (_, index) => const Divider(height: 0.6),
              itemBuilder: (_, index){
                UserBankEntity userBankEntity = provider?.userBankList?.elementAt(index)??UserBankEntity();
                _buildItem(userBankEntity,index);
              }
            );
          }
        )
      )
    );
  }

  Widget _buildItem(UserBankEntity userBankEntity,int index) {
    return InkWell(
      onTap: () => NavigatorUtils.goBackWithParams(context, userBankEntity),
      child: Container(
        padding: const EdgeInsets.symmetric(horizontal: 16.0),
        width: double.infinity,
        height: 74.0,
        alignment: Alignment.center,
        child: Row(
          children: <Widget>[
            CircleAvatar(
              radius: 24.0,
              backgroundColor: Colors.transparent,
              backgroundImage: ImageUtils.getImageProvider(Constant.imageServerHost+'${userBankEntity.cover}', holderImg: 'shop/tx'),
            ),
            Gaps.hGap16,
            Expanded(
              child: Column(
                mainAxisSize: MainAxisSize.min,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  Text(userBankEntity?.bankName??''),
                  Gaps.vGap8,
                  Text(userBankEntity?.cardUsername??'', style: TextStyles.textSize12),
                ],
              ),
            ),
            Visibility(
              visible: _selectIndex == index,
              child: const LoadAssetImage(
                'account/selected',
                height: 24.0,
                width: 24.0,
              ),
            )
          ],
        ),
      ),
    );
  }
}
